const db = require('../config/db')
const commonModel = '../schema/common_user'

const Mall = db.MallSequelize
const common_user = Mall.import(commonModel)

// 登陆
const login = async function(data) {
  const result = await common_user.findOne({
    where: {
      name: data.name
    }
  })
  return result
}
// 注册
const register = async (data) => {
  await common_user.create(
    {
      name: data.name,
      password: data.password,
      status: 1,
      image_url: 'http://localhost:8889/upload/profile.jpg'
    }
  )
  return true
}
// 获取用户信息
const getInfo = async (data) => {
    const result = await common_user.findOne({
      where: {
        name: data
      }
    })
    const obj = {
      id: result.id,
      name: result.name,
      password: result.password,
      pay_password: result.pay_password,
      nickName: result.nickName,
      gender: result.gender,
      address: result.address,
      message: result.message,
      roles: [result.role],
      balance: result.balance,
      image_url: result.image_url
    }
    return obj
}
// 修改用户信息
const updateInfo = async (name, value, column) => {
  console.log(value, column)
  if(column == 'nickname') {
    await common_user.update(
      {
        nickName: value
      },
      {
        where: {
          name: name
        }
      }
    )
    return true
  }
  if(column == 'gender') {
    await common_user.update(
      {
        gender: value
      },
      {
        where: {
          name: name
        }
      }
    )
    return true
  }
  if(column == 'address') {
    await common_user.update(
      {
        address: value
      },
      {
        where: {
          name: name
        }
      }
    )
    return true
  }
  if(column == 'message') {
    await common_user.update(
      {
        message: value
      },
      {
        where: {
          name: name
        }
      }
    )
    return true
  }
  if(column == 'image_url') {
    await common_user.update(
      {
        image_url: value
      },
      {
        where: {
          name: name
        }
      }
    )
    return true
  }
  return false
}
// 获取全部用户
const getCommonUsers = async (query) => {
  let page = Number(query.page)
  let limit = Number(query.limit)
  const result = await common_user.findAndCountAll(
    {
      offset: parseInt(page*10)*parseInt(limit/10),
      limit: limit,
      order: [['updated_at', 'desc']]
    }
  )
  return result
}
// 根据id获取用户
const getUserById = async (id) => {
  const result = await common_user.findAll(
    {
      where: {
        id: id
      }
    }
  )
  return result
}
// 根据搜索关键字获取用户
const getUserByKeyWords = async (keywords) => {
  if(keywords.id) {
    const result = await common_user.findAll(
      { 
        where: {
          id: Number(keywords.id)
        } 
      },
    )
    return result
  }else if(keywords.nickName) {
    const result = await common_user.findAll(
      { 
        where: {
          nickName: keywords.nickName
        } 
      },
    )
    return result
  }
}
// 根据id修改用户状态
const updateStatus = async (id, data) => {
  await common_user.update(
    {
      status: data.status
    },
    {
      where: {
        id: id
      }
    }
  )
  return true
}
// 修改个人余额
const updateBalance = async (data, name) => {
  await common_user.update(
    {
      balance: data.balance
    },
    {
      where: {
        name: name
      }
    }
  )
  return true
}
// 修改密码
const modifyPassword = async (data, name) => {
  await common_user.update(
    {
      password: data.password
    },
    {
      where: {
        name: name
      }
    }
  )
  return true
}
// 设置支付密码
const modifyPayPassword = async (data, name) => {
  await common_user.update(
    {
      pay_password: data.pay_password
    },
    {
      where: {
        name: name
      }
    }
  )
  return true
}
module.exports = {
  getCommonUsers,
  getUserByKeyWords,
  updateStatus,
  login,
  register,
  getInfo,
  getUserById,
  updateInfo,
  updateBalance,
  modifyPassword,
  modifyPayPassword
}
